<!--
* description: 采购单制作-备件明细查询
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      title="备件明细查询"
      :append-to-body="true"
      :visible.sync="showDialog"
      width="800px"
      @close="close"
    >
      <dialogHeader slot="title" title="备件明细查询" :styleType="$store.state.app.dialogType"></dialogHeader>
      <div>
        <div class="filter-container filter-params">
          <el-row :gutter="26">
            <component
              v-for="comp in tableComponents.filter(o => o.isMust === true)"
              :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
              :validrule="comp.validrule"
              :key="comp.compKey"
              :codeField="comp.codeField"
              :textField="comp.textField"
              :popupsKey="comp.compKey"
              :is="comp.component"
              :code="formField[comp.codeField]"
              :text="formField[comp.textField]"
              @changeCode="getComponentCode"
              @focusCode="getFocusCode"
              :disabled="comp.disabled"
              :isRequire="comp.isRequire"
              :span="comp.span || 8"
              :isMul="comp.isMul"
              :labelName="comp.labelName"
              :readonly="comp.readonly"
              :showPlaceholder="comp.showPlaceholder"
              :dateOptionsType="comp.dateOptionsType"
              :options="comp.options"
            ></component>
          </el-row>
        </div>
        <el-row :gutter="34" style="margin-bottom:5px;">
          <el-col :span="24">
            <div class="dialogTitle">
              <span class="dialogTitleButton">
                <el-button class="iconfont icon-search" @click="queryTable">查询</el-button>
                <el-button class="iconfont icon-reset" @click="reset">重置</el-button>
                <el-button class="el-icon-check" @click="confirm" :disabled="confirmDisabled">确认</el-button>
              </span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="26">
          <el-table
            :data="tableData"
            style="width: 100%"
            height="400"
            border
            @selection-change="onCheckSelectChange"
            v-loading="listLoading"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" width="60" align="center">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column class="sign" label="备件编号" prop="PART_NO" align="center"></el-table-column>
            <el-table-column label="备件名称" prop="PART_NAME" align="center"></el-table-column>
          </el-table>
          <el-pagination
            ref="oneTable.pageArea"
            background
            :page-sizes="this.$config.pageSizeArray"
            :page-size="this.$config.defaultPageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="prev, pager, next, jumper, sizes, ->, total"
            :total="pageTotal"
            :current-page.sync="listQuery.pageIndex"
            :pager-count="7"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import request from "@/utils/request";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  props: {
    isEditShow: { type: Boolean, default: false },
    editformField: { type: Object, default: {} },
    editTableData: { type: Array, default: [] }
  },
  data() {
    return {
      // 查询条件
      listQuery: {
        pageIndex: 1,
        pageSize: this.$config.defaultPageSize
      },
      pageTotal: 0,
      // 显示加载中
      listLoading: false,
      // 是否显示
      showDialog: this.isEditShow,
      // 表格数据
      tableData: [],

      // 选中的数据
      checkTableList: [],

      confirmDisabled: false,

      // 动态组件-查询条件
      tableComponents: [
        // {
        //   compKey: "editTable_compKey1",
        //   labelName: "备件编号",
        //   codeField: "PART_NO",
        //   component: () => import("@/components/wms/commonInput"),
        //   type: "inputText",
        //   isMust: true
        // },
         {
          compKey: "compKey2",
          labelName: "备件编号",
          codeField: "PART_NO", //['0','9]
          component: () => import("@/components/org/commonInput/textareaInput"),
          // type: "inputText",
          isMust: true
        },
        {
          compKey: "editTable_compKey2",
          labelName: "备件名称",
          codeField: "PART_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        }
      ],
      formField: {
        PART_NO: "",
        PART_NAME: ""
      },
      // callbackObj: {
      //    onBeforeQuery: queryObj => {
      //     queryObj.variables.PART_NO = queryObj.variables.PART_NO.trim()
      //       .split("\n")
      //       .toString();
      //    }
      // },
    };
  },
  created() {
    if (this.showDialog) {
      this.queryTable();
    }
  },
  unFormField: {},
      
  methods: {
    ccc() {
        this.queryTable();
    },
    confirm() {
      let partNoArr = [];
      this.checkTableList.forEach(item => {
        partNoArr.push(item.PART_NO);
        item.DQ_QTY = 0;
        item.AQ_QTY = 0;
        item.INBOUND_QTY = 0;
        item.OUTBOUND_QTY = 0;
      });
      this.confirmDisabled = true;
      request({
        url: "/mp/wm/vl/warehouse/purchase/queryPartNoStorage.do",
        method: "post",
        data: {
          WAREHOUSE_ID: this.editformField.WAREHOUSE_ID,
          PART_NOS: partNoArr.join(",")
        }
      }).then(res => {
        this.confirmDisabled = false;
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.forEach(item => {
            this.checkTableList.forEach(checkItem => {
              if (item.PART_NO == checkItem.PART_NO) {
                checkItem.DQ_QTY = item.DQ_QTY;
                checkItem.AQ_QTY = item.AQ_QTY;
                checkItem.INBOUND_QTY = item.INBOUND_QTY;
                checkItem.OUTBOUND_QTY = item.OUTBOUND_QTY;
              }
            });
          });
        }
        this.showDialog = false;
        this.$emit("confirmPartNo", this.checkTableList);
      });
    },
    //  当选择项发生变化时会触发该事件
    onCheckSelectChange(selection) {
      this.checkTableList = selection;
    },
    close() {
      this.showDialog = false;
      this.$emit("detailPartNoClose");
    },
    // 处理每页显示改变事件
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.queryTable();
    },
    // 当前页记录
    handleCurrentChange(val) {
      this.listQuery.pageIndex = val;
      this.queryTable();
    },
    queryTable() {
      let reqData = JSON.parse(JSON.stringify(this.listQuery));
      reqData.SUPPLIER_CODE = this.editformField.SUPPLIER_CODE;
      reqData.WAREHOUSE_ID = this.editformField.WAREHOUSE_ID;
      reqData.PART_NOS = [];
      this.editTableData.forEach(item => {
        reqData.PART_NOS.push(item.PART_NO);
      });
      reqData.PART_NAME = this.formField.PART_NAME
      reqData.PART_NOS = reqData.PART_NOS.join(",");

      reqData.PART_NOS_NEW = this.formField.PART_NO.trim()
            .split("\n")
            .toString();
            console.log(reqData.PART_NO);
      // Object.assign(reqData, this.formField);
      console.log(reqData.PART_NO);
      this.listLoading = true;
      request({
        url: "/mp/wm/vl/warehouse/purchase/queryPurchaseDPartNo.do",
        method: "post",
        data: reqData
      })
        .then(res => {
          this.listLoading = false;
          if (res.result === "1") {
            if (
              res.listResult &&
              res.listResult.rows &&
              res.listResult.rows.length > 0
            ) {
              this.tableData = res.listResult.rows;
              this.pageTotal = res.listResult.records;
            } else {
              this.tableData = [];
              this.pageTotal = 0;
            }
          } else {
            if (res.msg || res.debugMsg) {
              this.$message({
                message: res.debugMsg ? res.debugMsg : res.msg,
                uration: 2000
              });
            }
          }
        })
        .catch(err => {
          console.info(err);
          this.listLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.dialogTitle {
  text-align: left;
  overflow: hidden;
}
.dialogTitleButton {
  float: right;
}
.sign {
  width: 150px;
  height: 90px;
}
</style>